<?php

class PagesController extends AppController {

    var $name = 'Pages';
    var $helpers = array('Html', 'Session', 'Javascript', 'Link');
    var $uses = array('User');

    function beforeFilter() {
        $this->Auth->allow(array('display', 'home', 'index', 'advancesearch'));
        parent::beforeFilter();
    }

    function display() {
        echo __METHOD__;
        $path = func_get_args();

        if (!count($path)) {
            $path = $this->params;
        }
        $count = count($path);

        if (!$count) {
            $this->redirect('/');
        }
        $page = $subpage = $title_for_layout = null;

        if (isset($path['slug'])) {
            $page_slug = $path['slug'];
        }
        $page_details = $this->Page->findbySlug($page_slug);
        $title_for_layout = "WonderE | " . $page_details['Page']['page_title'] . "";
        $this->Set('title_for_layout', $title_for_layout);
        $this->set('keyword', $page_details['Page']['meta_description']);
        $this->set('description', $page_details['Page']['page_keywords']);
        $this->set('page_details', $page_details);

        /* if (!empty($path[0])) {
          $page = $path[0];

          }
          if (!empty($path[1])) {
          $subpage = $path[1];

          }
          if (!empty($path[$count - 1])) {
          $title_for_layout = Inflector::humanize($path[$count - 1]);

          }
          $vars_array = compact('page', 'subpage', 'title_for_layout');

          pr($vars_array);

          exit;

          $this->set(compact('page', 'subpage', 'title_for_layout'));

          $implode_path = implode('/', $path);
         */
        //echo "<br>Implode Path: ".$implode_path;
        //$this->render(implode('/', $path));
    }

    function home() {
        echo __METHOD__;
        $this->layout = "home";
        /* Start Categories Section */
        $categories = $this->_get_parent_categories();
        $x = 0;
        if (isset($categories) and ($categories)) {
            foreach ($categories as $category) {
                $subcats = $this->_sub_catecategories($category['Category']['id']);
                if (isset($subcats) and ($subcats)):
                    $categories[$x]['Category']['Subcategories'] = $subcats;
                    $c = 0;
                    foreach ($subcats as $subcatscategory) {
                        $_sub_catecategories = $this->_sub_catecategories($subcatscategory['Category']['id']);
                        if (isset($_sub_catecategories) and ($_sub_catecategories)):
                            $categories[$x]['Category']['Subcategories'][$c]['Category']['SubSubcategories'] = "true";
                        endif;
                        $c++;
                    }
                endif;
                $x++;
            }
            $this->Set('categories', $categories);
        }
        /* End Categories Section */
        /*
         * Best Selling Product
         */
        $bsp = $this->OrderItem->find('all', array(
            'conditions' => 'OrderItem.payment_status="1"',
            'fields' => 'DISTINCT OrderItem.product_id',
            'order' => 'OrderItem.id Desc'
                ));

        foreach ($bsp as $bspProducts) {
            $Product = $this->_getProducts('Product.id="' . $bspProducts['OrderItem']['product_id'] . '"');
            if (isset($Product[0]) and ($Product[0])) {
                $best_selling_products[] = $Product[0];
            }
        }
        //pr($best_selling_products);
        //exit;
        if (isset($best_selling_products) and ($best_selling_products)) {
            $this->Set('best_selling_products', $best_selling_products);
        }
        /*
         * End Bestselling Product
         */
    }

    function index() {
        $this->redirect(array('action' => 'home', 'controller' => 'Pages'));
    }

}
